package se.vgregion.icc.handlers;

import java.io.StringReader;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import se.riv.itintegration.dependencies.response.v1.ObjectFactory;
import se.riv.itintegration.dependencies.response.v1.SimpleDependencyResponse;
import se.vgregion.icc.handlers.annotations.DependencyResponseHandler;

/***
 * Handler to process unmarshal of SimpleDependencyResponse.
 * 
 * @author ricra2 & andan40
 * @see {@link se.riv.itintegration.dependencies.response.v1.SimpleDependencyResponse}
 * @see {@link se.vgregion.icc.handlers.annotations.DependencyResponseHandler}
 */
@DependencyResponseHandler
public final class SimpleDependencyResponseTypeHandler implements ResponseHandler {
    private static final Logger LOG = LoggerFactory.getLogger(SimpleDependencyResponseTypeHandler.class);

    private static final String SIMPLE_DEPENDENCY_RESPONSE = "SimpleDependencyResponse";

    /***
     * {@inheritDoc}
     */
    @Override
    public Object unmarshal(String xmlToUnmarshal) {
        SimpleDependencyResponse response = null;

        JAXBContext jaxbContext;
        try {
            jaxbContext = JAXBContext.newInstance(ObjectFactory.class);
            Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();

            @SuppressWarnings("unchecked")
            JAXBElement<SimpleDependencyResponse> unmarshal =
                (JAXBElement<SimpleDependencyResponse>) unmarshaller.unmarshal(new StringReader(xmlToUnmarshal));

            response = unmarshal.getValue();
        } catch (JAXBException e) {
            LOG.error("Couldn't unmarshal the xml String" + xmlToUnmarshal);
        }

        return response;
    }

    /***
     * {@inheritDoc}
     */
    @Override
    public String getHandledClassTagName() {
        return SIMPLE_DEPENDENCY_RESPONSE;
    }

}
